Coal Export Locations

About this page

This page provides code to create a basic Leaflet point map. A future version of this map/code will use county and tract level data that has been joined with Census demographic data.

Import libraries

library(tigris)
library(sf)
library(leaflet)
library(tidyverse)
library(janitor)
library(tidycensus)

Data import

In Excel, I filtered the original global dataset to only coal export locations in the United States. That is the dataset that is imported here. The original data can be found and downloaded here.

data = read.csv("data/coal-export-terminals-us.csv")

Download shapefiles for counties and tracts

# Download the census tract shapefiles for all states from tigris package

#census_tracts_us <- tracts(cb=TRUE)

#census_tracts_us <-  st_transform(census_tracts_us, 4269)

# save the tract level shapefiles to the working data directory
# commenting out since file exists
# st_write(census_tracts_us, "data/all_census_tracts_us.shp")

# we're repeating the above process but with counties
# keep the tract shapefiles in our data directory so we can do a more granular analysis locally

#census_counties_us <- counties(cb=TRUE)
#census_counties_us <-  st_transform(census_counties_us, 4269)

# save county-level shapes to the working data directory
# commenting out since file exists
#st_write(census_counties_us, "data/all_census_counties_us.shp")

Initialize a point map with Leaflet

# Load point data

points_sf <- st_as_sf(data, coords = c("Longitude", "Latitude"), crs = 4269)

# Load census tract shapefiles -- uncomment if you want a tract-level map
#census_tracts <- st_read("data/all_census_tracts_us.shp")

# Load census county shapefiles
census_counties <- st_read("data/all_census_counties_us.shp")

Draw the map

map <- leaflet(options = leafletOptions(preferCanvas = TRUE)) %>%
  addTiles() %>%
  setView(lng = -96.25, lat = 39.50, zoom = 4) %>% 
  addPolygons(data = census_counties,
              fillColor = "lightgray",
              color = "gray",
              weight = 1,
              fillOpacity = 0.5) %>% 
  addCircleMarkers(
              data = points_sf,
             color = "red",
             opacity = 0.8,
             weight = 2,
             radius = 5)
map